package com.ynunicom.dc.dingdingcontractapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.dto.ResponseDto;
import com.ynunicom.dc.dingdingcontractapp.entity.StandReturnerImpl;
import com.ynunicom.dc.dingdingcontractapp.entity.entityinterface.StandReturner;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.DeptRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.GetChildDeptIdListRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.GetDeptListRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.TransBody;
import com.ynunicom.dc.dingdingcontractapp.service.DeptService;
import com.ynunicom.dc.dingdingcontractapp.service.impl.DeptServiceImpl;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: jinye.Bai
 * @date: 2020/5/27 14:34
 */
@RequestMapping("/dept")
@RestController
public class DeptController {

    @Resource
    DeptService deptService;

    @Resource
    StandReturner standReturner;

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @PostMapping("/create")
    public ResponseDto<JSONObject> create(@RequestParam("access_token") String access_token, @RequestBody DeptRequestBody deptRequestBody){
        TransBody<DeptRequestBody> transBody = new TransBody<>(deptRequestBody,access_token);
        return standReturner.doReturn(transBody,deptService.getClass(),deptService.getClass().getMethod("addNewDept",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @PostMapping("/update")
    public ResponseDto<JSONObject> update(@RequestBody DeptRequestBody deptRequestBody,@RequestParam("access_token") String access_token){
        TransBody<DeptRequestBody> transBody = new TransBody<>(deptRequestBody,access_token);
        return standReturner.doReturn(transBody,deptService.getClass(),deptService.getClass().getMethod("updateDept",TransBody.class));
    }

    /**
     *
     * @param id 传入父部门ID，根部门ID为1
     * @param access_token
     * @return
     */
    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/childIdList")
    public ResponseDto<JSONObject> childIdList(@RequestParam("id") String id, @RequestParam("access_token") String access_token){
        GetChildDeptIdListRequestBody getChildDeptIdListRequestBody = new GetChildDeptIdListRequestBody(id);
        TransBody<GetChildDeptIdListRequestBody> transBody = new TransBody<>(getChildDeptIdListRequestBody,access_token);
        return standReturner.doReturn(transBody,deptService.getClass(),deptService.getClass().getMethod("getChildDeptIdList",TransBody.class));
    }


    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/list")
    public ResponseDto<JSONObject> list(@RequestParam(value = "fetch_child",required = false)String fetch_child,@RequestParam(value = "id",required = false)String id, @RequestParam("access_token") String access_token){
        GetDeptListRequestBody getDeptListRequestBody;
        if (fetch_child!=null&& id!=null){
            getDeptListRequestBody = new GetDeptListRequestBody(fetch_child,id);
        }
        else if (fetch_child!=null){
            getDeptListRequestBody = new GetDeptListRequestBody(fetch_child,"");
        }
        else if (id!=null){
            getDeptListRequestBody = new GetDeptListRequestBody("",id);
        }
        else {
            getDeptListRequestBody = new GetDeptListRequestBody("","");
        }
        TransBody<GetDeptListRequestBody> transBody = new TransBody<>(getDeptListRequestBody,access_token);
        return standReturner.doReturn(transBody,deptService.getClass(),deptService.getClass().getMethod("list",TransBody.class));
    }


    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/info")
    public ResponseDto<JSONObject> info(@RequestParam("id") String id, @RequestParam("access_token") String access_token){
        TransBody<String> transBody = new TransBody<>(id,access_token);
        return standReturner.doReturn(transBody,deptService.getClass(),deptService.getClass().getMethod("info",TransBody.class));
    }

}
